﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.Reflection;
using Gestalt.Attributes;
using Gestalt.Interfaces;
using Utilities.DataTypes.Patterns.BaseClasses;
using System.Text;
#endregion

namespace Gestalt
{
    /// <summary>
    /// Config manager
    /// </summary>
    public class Manager : Singleton<Manager>
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        protected Manager()
            : base()
        {
            ConfigFiles = new Dictionary<string, IConfig>();
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Registers a config file
        /// </summary>
        /// <param name="Name">Name to reference by</param>
        /// <param name="ConfigObject">Config object to register</param>
        public void RegisterConfigFile(string Name, IConfig ConfigObject)
        {
            if (ConfigFiles.ContainsKey(Name))
                return;
            ConfigObject.Load();
            ConfigFiles.Add(Name, ConfigObject);
        }

        /// <summary>
        /// Registers all config files in an assembly
        /// </summary>
        /// <param name="AssemblyContainingConfig">Assembly to search</param>
        public void RegisterConfigFile(Assembly AssemblyContainingConfig)
        {
            List<Type> Types = Utilities.Reflection.Reflection.GetTypes(AssemblyContainingConfig, "IConfig");
            foreach (Type Temp in Types)
            {
                if (!Temp.ContainsGenericParameters)
                {
                    string Name = "";
                    object[] Attributes = Temp.GetCustomAttributes(typeof(ConfigAttribute), true);
                    if (Attributes.Length > 0)
                    {
                        Name = ((ConfigAttribute)Attributes[0]).Name;
                    }
                    IConfig TempConfig = (IConfig)Temp.Assembly.CreateInstance(Temp.FullName);
                    RegisterConfigFile(Name, TempConfig);
                }
            }
        }

        /// <summary>
        /// Registers all config files in an assembly that is not currently loaded
        /// </summary>
        /// <param name="AssemblyLocation">Location of the assembly</param>
        public void RegisterConfigFile(string AssemblyLocation)
        {
            RegisterConfigFile(Assembly.LoadFile(AssemblyLocation));
        }

        /// <summary>
        /// Gets a specified config file
        /// </summary>
        /// <typeparam name="T">Type of the config object</typeparam>
        /// <param name="Name">Name of the config object</param>
        /// <returns>The config object specified</returns>
        public T GetConfigFile<T>(string Name)
        {
            if (!ConfigFiles.ContainsKey(Name))
                throw new Exception("The config object " + Name + " was not found.");
            if (!(ConfigFiles[Name] is T))
                throw new Exception("The config object " + Name + " is not the specified type.");
            return (T)ConfigFiles[Name];
        }

        /// <summary>
        /// Determines if a specified config file is registered
        /// </summary>
        /// <typeparam name="T">Type of the config object</typeparam>
        /// <param name="Name">Name of the config object</param>
        /// <returns>The config object specified</returns>
        public bool ContainsConfigFile<T>(string Name)
        {
            if (!ConfigFiles.ContainsKey(Name))
                return false;
            if (!(ConfigFiles[Name] is T))
                return false;
            return true;
        }

        public override string ToString()
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<ul>").Append("<li>").Append(ConfigFiles.Count).Append("</li>");
            foreach (string Name in ConfigFiles.Keys)
            {
                Builder.Append("<li>").Append(Name).Append(":").Append(ConfigFiles[Name].GetType().FullName).Append("</li>");
            }
            Builder.Append("</ul>");
            return Builder.ToString();
        }

        #endregion

        #region Private properties

        private static Dictionary<string, IConfig> ConfigFiles { get; set; }

        #endregion
    }
}